﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Common.Extend
{
    public static class ExDataReader
    {
        /// <summary>
        /// SqlDataReader转Model实体类
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="reader"></param>
        /// <returns></returns>
        public static T ExToModel<T>(this System.Data.Common.DbDataReader reader) where T : new()
        {
            if (reader.Read())
            {
                Type type = typeof(T);
                T m = Activator.CreateInstance<T>();
                object val = null;


                PropertyInfo[] pros = type.GetProperties(BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
                foreach (PropertyInfo pro in pros)
                {
                    var value = reader[pro.Name];
                    if (value != null && !(value is DBNull))
                    {
                        if (pro.PropertyType.IsGenericType && pro.PropertyType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
                        {
                            if (value == null)
                                val = null;
                            NullableConverter nullableConverter = new NullableConverter(pro.PropertyType);
                            val = Convert.ChangeType(value, nullableConverter.UnderlyingType);
                        }
                        else
                            val = value;
                        pro.SetValue(m, val, null);
                    }
                }


                //for (int i = 0; i < reader.FieldCount; i++)
                //{
                //    //foreach (var value in reader)
                //    var value = reader[i];
                //    if (value != null && !(value is DBNull))
                //    {
                //        PropertyInfo pro = type.GetProperty(reader[i].GetType().Name, BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
                //        if (pro != null)
                //        {
                //            if (pro.PropertyType.IsGenericType && pro.PropertyType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
                //            {
                //                if (value == null)
                //                    val = null;
                //                NullableConverter nullableConverter = new NullableConverter(pro.PropertyType);
                //                val = Convert.ChangeType(value, nullableConverter.UnderlyingType);
                //            }
                //            pro.SetValue(m, val, null);
                //        }
                //    }
                //}
                return m;
            }
            return default(T);
        }
    }
}
